package ac.zlf.springmybatis.core.util.image;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.awt.image.ConvolveOp;
import java.awt.image.Kernel;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;

public class ImageScaler {
    public static void scale(File imagePath, File destFile, float scale)
            throws IOException {
        Image image = ImageIO.read(imagePath);
        scale(image, destFile, scale);
    }

    public static void scale(File imagePath, File destFile, int preferredWidth, int preferredHeight)
            throws IOException {
        Image image = ImageIO.read(imagePath);
        scaleImage(destFile, preferredWidth, preferredHeight, image);
    }

    public static void scale(Image image, File destFile, float scale)
            throws IOException {
        int preferredWidth = (int) (scale * image.getWidth(null));
        int preferredHeight = (int) (scale * image.getHeight(null));
        scaleImage(destFile, preferredWidth, preferredHeight, image);
    }

    public static void scale(Image image, File destFile, int preferredWidth, int preferredHeight)
            throws IOException {
        scaleImage(destFile, preferredWidth, preferredHeight, image);
    }

    private static void scaleImage(File destFile, int preferredWidth, int preferredHeight, Image originalImage)
            throws IOException {
        ImageIO.write(scaleImage(preferredWidth, originalImage), "jpg", destFile);
    }

    public static BufferedImage scaleImage(int preferredWidth, Image originalImage) {
        Image scaleImage = originalImage.getScaledInstance(preferredWidth, -1, 2);
        int width = scaleImage.getWidth(null);
        int height = scaleImage.getHeight(null);
        BufferedImage buffer = new BufferedImage(width, height, 1);
        buffer.getGraphics().drawImage(scaleImage, 0, 0, width, height, null);
        return buffer;
    }

    public static BufferedImage scaleImage(int preferredWidth, int preferredHeight, Image originalImage) {
        Image scaleImage = originalImage.getScaledInstance(preferredWidth, preferredHeight, 16);
        int width = scaleImage.getWidth(null);
        int height = scaleImage.getHeight(null);
        BufferedImage buffer = new BufferedImage(width, height, 1);
        Graphics2D g2 = buffer.createGraphics();
        g2.drawImage(scaleImage, 0, 0, width, height, Color.white, null);
        g2.dispose();
        float[] kernelData2 = {-0.125F, -0.125F, -0.125F, -0.125F, 2.0F, -0.125F, -0.125F, -0.125F, -0.125F};
        Kernel kernel = new Kernel(3, 3, kernelData2);
        ConvolveOp cOp = new ConvolveOp(kernel, 1, null);
        buffer = cOp.filter(buffer, null);
        return buffer;
    }
}



/* Location:           D:\repository\cc\hwtech\core\2.0.1-school-SNAPSHOT\core-2.0.1-school-SNAPSHOT.jar

 * Qualified Name:     ImageScaler

 * JD-Core Version:    0.7.0.1

 */
